<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
      <BasicForm @register="registerForm">
        <template #config="{model, field}">
          <a-select v-model:value="model[field]"
                  :field-names="{ label: 'no', value: 'id' }"
                  placeholder="请选择配置"
                  :options="optionsData">
          </a-select>
        </template>
        <template #growPlan="{model, field}">
          <a-select v-model:value="model[field]"
                    placeholder="请选择种植计划">
            <a-select-option
              v-for="option in optionsGrowData"
              :key="option.id"
              :value="option.id">
              <span>{{ option.id }}</span>&nbsp;&nbsp;
              <a-tag color="green" v-for="tag in option.growPlanProductPlotJsons" :key="tag.productNo">{{ tag.productNo_dictText }}</a-tag>
            </a-select-option>
          </a-select>
        </template>

      </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import { BasicTree, } from '/@/components/Tree/index';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form';
    import {formSchema} from '../QrcodeManage.data';
    import {list} from '../../qrcodeconfig/QrcodeConfig.api'
    import {saveOrUpdate} from '../QrcodeManage.api';
    import {defHttp} from "/@/utils/http/axios";
    import { Select } from 'ant-design-vue';

    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    // 配置选项数据
    const optionsData = ref([]);
    // 种植计划选项数据
    const optionsGrowData = ref([]);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    // 获取配置选项数据
    function  getOptions(value){
      defHttp.get({url: "/qrcodeConfig/list", params: {'no': value,'pageSize':1000}})
          .then(res => {
            console.log(res);
            if (res&&res.records) {
              console.log("res:", res); // 注意添加标签以便识别
              res.records.forEach(record =>{
                if (record.productNo_dictText){
                    record.no = record.no+"("+record.productNo_dictText+")";
                }
              })
              console.log("reschange:", res); // 注意添加标签以便识别
              optionsData.value = res.records;
              console.log("值：",optionsData.value);
            }
          })
    }
    // 获取种植计划数据
    function  getOptionsGrowData(value){
      defHttp.get({url: "/plan/list", params: {'id': value,'pageSize':10000}})
        .then(res => {
          if (res&&res.records) {
            res.records.forEach(record =>{
              if (record.id){
                let son = record.growPlanProductPlotJsons.map(item => item.productNo_dictText);
                record.show = { id: record.id, tags: son };
              }
            })
            optionsGrowData.value = res.records;
          }
        })
    }
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        // 获取选项数据
        await getOptions();
        // 获取种植计划
        await getOptionsGrowData();
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
